package com.jwl.idc.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jwl.idc.R;
import com.jwl.idc.actionbean.BaseBean;
import com.jwl.idc.actionbean.UserBean;
import com.jwl.idc.customview.DialogCreater;
import com.jwl.idc.customview.TitleBar;
import com.jwl.idc.database.LocalDBManager;
import com.jwl.idc.http.HttpAction;
import com.jwl.idc.http.HttpToViewInterface;
import com.jwl.idc.utils.StringUtils;

/**
 * Created by liangfan on 2015/12/31.
 */


public class LoginActivity extends BaseActivity implements View.OnClickListener{

    private Button regiserUserButton,loginUserBt;
    private EditText phoneNumberEt,pwdEt;
    private Intent intent;
    public TitleBar titlebar;
    private TextView forgotPwdTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        regiserUserButton = (Button) findViewById(R.id.registerBt);
        loginUserBt = (Button) findViewById(R.id.loginBt);
        forgotPwdTv = (TextView) findViewById(R.id.forget_password);
        phoneNumberEt = (EditText) findViewById(R.id.inputPhoneNumberEt);
        pwdEt = (EditText) findViewById(R.id.pwdEt);
        regiserUserButton.setOnClickListener(this);
        loginUserBt.setOnClickListener(this);
        intent = new Intent(this, RegisterUserActivity.class);
        titlebar = (TitleBar) findViewById(R.id.titleBar);
        titlebar.setText("",getString(R.string.login), "");


    }


    void login(String phoneNumber,String pwd){
        httpToviewCallBack = new HttpToViewInterface() {
            @Override
            public void callBackData(BaseBean data) {
                UserBean userBean = (UserBean)data;
                LocalDBManager.deleteUserInfo(LoginActivity.this,userBean.getData().getUser().getId());
                LocalDBManager.insertUserInfo(LoginActivity.this,userBean.getData());
                startActivity(new Intent(LoginActivity.this,HomeActivty.class));
            }
            @Override
            public void finish() {
                DialogCreater.closeDialog(BaseActivity.LOGIN_DIALOG);
            }
        };
        action = new HttpAction(httpToviewCallBack, this);
        action.login(phoneNumber, pwd);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.registerBt:
                startActivity(intent);
                break;
            case R.id.loginBt:
                String phoneNumber = phoneNumberEt.getText().toString().trim();
                String pwd = pwdEt.getText().toString().trim();
                if(StringUtils.checkPhoneNumber(phoneNumber,this) && StringUtils.checkPwd(pwd,this)){
                    login(phoneNumber,pwd);
                    showWaitDialog(LOGIN_DIALOG,getString(R.string.loginning));
                };
                break;
            case R.id.forget_password:
                break;
        }

    }
}
